<template>
  <div class="pending-orders">
    <!-- 页面标题 -->
    <div class="page-header">
      <div class="header-left">
        <h2 class="page-title">待处理订单</h2>
        <p class="page-desc">管理所有待处理的物流订单</p>
      </div>
      <div class="header-right">
        <el-button type="primary" @click="handleBatchProcess">
          <el-icon><Operation /></el-icon>
          批量处理
        </el-button>
        <el-button @click="handleRefresh">
          <el-icon><Refresh /></el-icon>
          刷新
        </el-button>
      </div>
    </div>

    <!-- 统计卡片 -->
    <div class="stats-cards">
      <el-card class="stat-card">
        <div class="stat-content">
          <div class="stat-icon pending">
            <el-icon><Clock /></el-icon>
          </div>
          <div class="stat-info">
            <div class="stat-number">{{ stats.pending }}</div>
            <div class="stat-label">待处理</div>
          </div>
        </div>
      </el-card>
      <el-card class="stat-card">
        <div class="stat-content">
          <div class="stat-icon urgent">
            <el-icon><Warning /></el-icon>
          </div>
          <div class="stat-info">
            <div class="stat-number">{{ stats.urgent }}</div>
            <div class="stat-label">紧急订单</div>
          </div>
        </div>
      </el-card>
      <el-card class="stat-card">
        <div class="stat-content">
          <div class="stat-icon today">
            <el-icon><Calendar /></el-icon>
          </div>
          <div class="stat-info">
            <div class="stat-number">{{ stats.today }}</div>
            <div class="stat-label">今日新增</div>
          </div>
        </div>
      </el-card>
      <el-card class="stat-card">
        <div class="stat-content">
          <div class="stat-icon processed">
            <el-icon><Check /></el-icon>
          </div>
          <div class="stat-info">
            <div class="stat-number">{{ stats.processed }}</div>
            <div class="stat-label">已处理</div>
          </div>
        </div>
      </el-card>
    </div>

    <!-- 搜索和筛选 -->
    <el-card class="search-card">
      <div class="search-form">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-input
              v-model="searchForm.orderNo"
              placeholder="订单号"
              clearable
              prefix-icon="Search"
            />
          </el-col>
          <el-col :span="6">
            <el-input
              v-model="searchForm.customerName"
              placeholder="客户姓名"
              clearable
              prefix-icon="User"
            />
          </el-col>
          <el-col :span="6">
            <el-select
              v-model="searchForm.priority"
              placeholder="优先级"
              clearable
              style="width: 100%"
            >
              <el-option label="普通" value="normal" />
              <el-option label="紧急" value="urgent" />
              <el-option label="特急" value="super_urgent" />
            </el-select>
          </el-col>
          <el-col :span="6">
            <el-date-picker
              v-model="searchForm.dateRange"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              style="width: 100%"
            />
          </el-col>
        </el-row>
        <div class="search-actions">
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>
            搜索
          </el-button>
          <el-button @click="handleReset">
            <el-icon><Refresh /></el-icon>
            重置
          </el-button>
        </div>
      </div>
    </el-card>

    <!-- 订单列表 -->
    <el-card class="table-card">
      <template #header>
        <div class="table-header">
          <span>订单列表</span>
          <div class="table-actions">
            <el-button size="small" @click="handleExport">
              <el-icon><Download /></el-icon>
              导出
            </el-button>
          </div>
        </div>
      </template>
      
      <el-table
        :data="orderList"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        v-loading="loading"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="orderNo" label="订单号" width="180">
          <template #default="{ row }">
            <el-link type="primary" @click="handleViewOrder(row)">
              {{ row.orderNo }}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column prop="customerName" label="客户姓名" width="120" />
        <el-table-column prop="phone" label="联系电话" width="140" />
        <el-table-column prop="address" label="收货地址" min-width="200" />
        <el-table-column prop="priority" label="优先级" width="100">
          <template #default="{ row }">
            <el-tag
              :type="getPriorityType(row.priority)"
              size="small"
            >
              {{ getPriorityText(row.priority) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="amount" label="金额" width="100">
          <template #default="{ row }">
            ¥{{ row.amount }}
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button size="small" type="primary" @click="handleProcess(row)">
              处理
            </el-button>
            <el-button size="small" @click="handleAssign(row)">
              分配
            </el-button>
            <el-button size="small" type="danger" @click="handleCancel(row)">
              取消
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-wrapper">
        <el-pagination
          v-model:current-page="pagination.current"
          v-model:page-size="pagination.size"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  Operation,
  Refresh,
  Clock,
  Warning,
  Calendar,
  Check,
  Search,
  User,
  Download
} from '@element-plus/icons-vue'

// 统计数据
const stats = reactive({
  pending: 156,
  urgent: 23,
  today: 45,
  processed: 89
})

// 搜索表单
const searchForm = reactive({
  orderNo: '',
  customerName: '',
  priority: '',
  dateRange: []
})

// 分页
const pagination = reactive({
  current: 1,
  size: 20,
  total: 0
})

// 订单列表
const orderList = ref([])
const loading = ref(false)
const selectedOrders = ref([])

// 模拟数据
const mockOrders = [
  {
    id: 1,
    orderNo: 'WL20241201001',
    customerName: '张三',
    phone: '13800138001',
    address: '北京市朝阳区建国路88号',
    priority: 'urgent',
    amount: 128.50,
    createTime: '2024-12-01 09:30:00'
  },
  {
    id: 2,
    orderNo: 'WL20241201002',
    customerName: '李四',
    phone: '13800138002',
    address: '上海市浦东新区陆家嘴金融中心',
    priority: 'normal',
    amount: 256.00,
    createTime: '2024-12-01 10:15:00'
  },
  {
    id: 3,
    orderNo: 'WL20241201003',
    customerName: '王五',
    phone: '13800138003',
    address: '广州市天河区珠江新城',
    priority: 'super_urgent',
    amount: 89.90,
    createTime: '2024-12-01 11:00:00'
  }
]

// 获取优先级类型
const getPriorityType = (priority) => {
  const types = {
    normal: '',
    urgent: 'warning',
    super_urgent: 'danger'
  }
  return types[priority] || ''
}

// 获取优先级文本
const getPriorityText = (priority) => {
  const texts = {
    normal: '普通',
    urgent: '紧急',
    super_urgent: '特急'
  }
  return texts[priority] || '普通'
}

// 加载数据
const loadData = async () => {
  loading.value = true
  try {
    // 模拟API调用
    await new Promise(resolve => setTimeout(resolve, 1000))
    orderList.value = mockOrders
    pagination.total = mockOrders.length
  } catch (error) {
    ElMessage.error('加载数据失败')
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  pagination.current = 1
  loadData()
}

// 重置搜索
const handleReset = () => {
  Object.assign(searchForm, {
    orderNo: '',
    customerName: '',
    priority: '',
    dateRange: []
  })
  handleSearch()
}

// 刷新
const handleRefresh = () => {
  loadData()
}

// 批量处理
const handleBatchProcess = () => {
  if (selectedOrders.value.length === 0) {
    ElMessage.warning('请选择要处理的订单')
    return
  }
  ElMessageBox.confirm(
    `确定要处理选中的 ${selectedOrders.value.length} 个订单吗？`,
    '确认处理',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    ElMessage.success('批量处理成功')
  })
}

// 导出
const handleExport = () => {
  ElMessage.success('导出成功')
}

// 查看订单详情
const handleViewOrder = (row) => {
  ElMessage.info(`查看订单：${row.orderNo}`)
}

// 处理订单
const handleProcess = (row) => {
  ElMessageBox.confirm(
    `确定要处理订单 ${row.orderNo} 吗？`,
    '确认处理',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    ElMessage.success('处理成功')
  })
}

// 分配订单
const handleAssign = (row) => {
  ElMessage.info(`分配订单：${row.orderNo}`)
}

// 取消订单
const handleCancel = (row) => {
  ElMessageBox.confirm(
    `确定要取消订单 ${row.orderNo} 吗？`,
    '确认取消',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    ElMessage.success('取消成功')
  })
}

// 选择变化
const handleSelectionChange = (selection) => {
  selectedOrders.value = selection
}

// 分页大小变化
const handleSizeChange = (size) => {
  pagination.size = size
  loadData()
}

// 当前页变化
const handleCurrentChange = (current) => {
  pagination.current = current
  loadData()
}

onMounted(() => {
  loadData()
})
</script>

<style scoped>
.pending-orders {
  padding: 0;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 24px;
}

.page-title {
  margin: 0 0 8px 0;
  font-size: 24px;
  font-weight: 600;
  color: #1f2937;
}

.page-desc {
  margin: 0;
  color: #6b7280;
  font-size: 14px;
}

.header-right {
  display: flex;
  gap: 12px;
}

.stats-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
  margin-bottom: 24px;
}

.stat-card {
  border: none;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.stat-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.stat-content {
  display: flex;
  align-items: center;
  gap: 16px;
}

.stat-icon {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  color: white;
}

.stat-icon.pending {
  background: linear-gradient(135deg, #3b82f6, #1d4ed8);
}

.stat-icon.urgent {
  background: linear-gradient(135deg, #f59e0b, #d97706);
}

.stat-icon.today {
  background: linear-gradient(135deg, #10b981, #059669);
}

.stat-icon.processed {
  background: linear-gradient(135deg, #8b5cf6, #7c3aed);
}

.stat-number {
  font-size: 28px;
  font-weight: 700;
  color: #1f2937;
  line-height: 1;
}

.stat-label {
  font-size: 14px;
  color: #6b7280;
  margin-top: 4px;
}

.search-card {
  margin-bottom: 24px;
  border: none;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.search-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.search-actions {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
}

.table-card {
  border: none;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.table-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 600;
  color: #1f2937;
}

.table-actions {
  display: flex;
  gap: 8px;
}

.pagination-wrapper {
  display: flex;
  justify-content: center;
  margin-top: 24px;
  padding-top: 24px;
  border-top: 1px solid #e5e7eb;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .page-header {
    flex-direction: column;
    gap: 16px;
  }
  
  .stats-cards {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .search-form .el-row {
    margin: 0;
  }
  
  .search-form .el-col {
    padding: 0;
    margin-bottom: 12px;
  }
}
</style> 